import uix
import uiutil
import xtriui
import uiconst
import uthread
import blue
import uicls

class PopupNotification(uicls.Container):
    __guid__ = 'xtriui.PopupNotification'

    def Startup(self):
        self.state = uiconst.UI_NORMAL
        self.shouldKill = False
        sub = uicls.Container(name='sub', parent=self, align=uiconst.TOALL, pos=(0, 0, 0, 0), state=uiconst.UI_DISABLED)
        uicls.Frame(parent=self)
        uicls.Fill(parent=self, color=(0, 0, 0, 1), idx=-1, state=uiconst.UI_DISABLED)
        closex = uicls.Icon(icon='ui_38_16_220', parent=self, idx=0, state=uiconst.UI_NORMAL, align=uiconst.TOPRIGHT)
        closex.OnClick = self.CloseNotification
        closex.sr.hint = mls.UI_SHARED_SKILLS_CLOSENOTIFICATION
        iconCont = uicls.Container(name='iconCont', parent=sub, align=uiconst.TOLEFT, pos=(0, 0, 60, 0))
        textCont = uicls.Container(name='textCont', parent=sub, align=uiconst.TOALL, pos=(0, 0, 0, 0), state=uiconst.UI_NORMAL)
        self.sr.icon = uicls.Sprite(parent=iconCont, pos=(-2, 2, 64, 64), align=uiconst.CENTERLEFT, lockAspect=1, state=uiconst.UI_DISABLED)
        self.sr.icon.tripass = eve.rot.GetInstance('res:/ui/pass/linear.blue')
        uix.MapSprite('50_11', self.sr.icon)
        self.sr.headerText = uicls.Label(text='', parent=textCont, left=0, top=12, align=uiconst.TOALL, fontsize=10, letterspace=1, linespace=10, uppercase=1, state=uiconst.UI_DISABLED, autowidth=False, autoheight=False)
        self.sr.text1 = uicls.Label(text='', parent=textCont, pos=(0, 24, 0, 0), autowidth=0, autoheight=1, state=uiconst.UI_DISABLED, align=uiconst.TOTOP)
        self.sr.text2 = uicls.Label(text='', parent=textCont, pos=(0, 0, 0, 0), autowidth=0, autoheight=1, state=uiconst.UI_HIDDEN, align=uiconst.TOTOP, padding=(0, -2, 0, 0))
        self.sr.text3 = uicls.Label(text='', parent=textCont, pos=(0, 0, 0, 0), autowidth=0, autoheight=1, state=uiconst.UI_HIDDEN, align=uiconst.TOTOP, padding=(0, -2, 0, 0))



    def Load(self, data, *args):
        iconNum = getattr(data, 'iconNum', None)
        if iconNum:
            uix.MapSprite(iconNum, self.sr.icon)
        headerText = getattr(data, 'headerText', '')
        self.sr.headerText.text = headerText
        top = ((self.sr.headerText.top + self.sr.headerText.textheight) - 2)
        text1 = getattr(data, 'text1', '')
        self.sr.text1.text = text1
        self.sr.text1.top = top
        top += self.sr.text1.textheight
        text2 = getattr(data, 'text2', '')
        if text2:
            self.sr.text2.text = text2
            self.sr.text2.state = uiconst.UI_DISABLED
            top += self.sr.text2.textheight
        text3 = getattr(data, 'text3', '')
        if text3:
            self.sr.text3.text = text3
            self.sr.text3.state = uiconst.UI_DISABLED
            top += self.sr.text3.textheight
        self.height = max(self.height, (top + 6))
        time = getattr(data, 'time', 10000)
        uthread.new(self.Kill, time)



    def Kill(self, time = 10000):
        blue.pyos.synchro.Sleep(time)
        if ((not self) or self.destroyed):
            return 
        if ((eve.triapp.uilib.mouseOver == self) or uiutil.IsUnder(eve.triapp.uilib.mouseOver, self)):
            self.shouldKill = True
            return 
        self.CloseNotification()



    def CloseNotification(self, *args):
        self.state = uix.UI_HIDDEN
        self.Close()



    def OnMouseExit(self, *args):
        if self.shouldKill:
            self.CloseNotification()




